//
//  LDEditMyResumeVC.swift
//  DingDongZP
//
//  Created by LeMac on 16/12/29.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

enum LDEditMyResumeVCType {
    //微简历的4种编辑状态
    case Default
    case HeigthLightStrength
    case WorkExp
    case EduExp
}

class LDEditMyResumeVC: LDBaseCustomizeVC {
    
    let titleArr:Array<String> = ["语音微简历","文字微简历"]
    var voiceResumeBtn:UIButton?
    var textResumeBtn:UIButton?
    var gampView:UIView?
    
    var line1:UIView?
    var bottomLine:UIView?
    
    let BtnW = LDFrame.ScreenWidth/2
    let BtnH:CGFloat = 45
    
    //语音／text 微简历
    var type:MyResumeType?
    //编辑类型
    var editType:LDEditMyResumeVCType?
    
    var childVC:Array<AnyObject> = []
    let btnsTag:Int = 999
    
    var contentView:UIScrollView?
    var selectTitleButton = UIButton()
    var preTitleButton:UIButton?
    
    var resumeDict = NSDictionary()
    var rootNav:LDRootNavVC?
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        setDefalutContent()
        setupContentVC()
        requestNet()
    }
    
    override func viewWillAppear(animated: Bool) {
        
        //        super.viewWillAppear(animated)
        rdv_tabBarController.setTabBarHidden(true, animated: false)
        UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseInOut, animations: {
            [weak self] in
            self?.contentView?.contentOffset.y = 0
            }, completion: nil)
        
    }
    
    
    func requestNet() {
        
        LDUserInfo.sharedInstance.queryJobFinderMyResume({ [weak self] (dict) in
            
            self?.resumeDict = dict
            //查询文字余音 微简历
            let titleStr = dict["isresumeaudio"] as? String
            
            if titleStr == "1" {
                //第一次选中
                self?.clickBtn((self?.voiceResumeBtn!)!)
                self?.type = .Voice
                
            } else if titleStr == "0" {
                
                self?.clickBtn((self?.textResumeBtn!)!)
                self?.type = .Text
            }
            }, failure: nil)
    }
    
    func changeResumeStatus(changeType:MyResumeType) {
        
        let title = (type?.rawValue == 1) ? "当前为语音微简历" : "当前为文字微简历"
        
        showLDAlertView(title, sure: { Void in
            
            LDUserInfoNet.sharedInstance.changeMyResumeVoiceORText(changeType, success: {
                (status) in
                
                LDShowHUB.shareInstance.showSucc("切换成功")
                //                self?.navigationController?.popViewControllerAnimated(true)
                
                }, failure: {
                    (error) in
                    
                    LDShowHUB.shareInstance.showSucc("切换失败")
                    //                    self?.navigationController?.popViewControllerAnimated(true)
            })
            }, cancel: nil)
    }
    //
    func setDefalutContent() {
        
//        title = "编辑微简历"
        gampView = UIView.init()
        gampView?.backgroundColor = LDGolbalColor.White
        
        contentView = UIScrollView.init(frame:CGRectMake(0, 0, LDFrame.ScreenWidth, LDFrame.ScreenHeight - LDFrame.NavH))
        contentView?.contentInset = UIEdgeInsetsMake(0, 0, -LDFrame.NavH, 0)
        contentView?.contentSize = CGSizeMake(LDFrame.ScreenWidth * CGFloat(titleArr.count), LDFrame.ScreenHeight - LDFrame.NavH - BtnH)
        contentView?.translatesAutoresizingMaskIntoConstraints = false
        contentView?.pagingEnabled = true
        contentView?.delegate = self
        contentView?.backgroundColor = LDGolbalColor.BgGoundGray //
        contentView?.bounces = false
        
        voiceResumeBtn = UIButton.setDetailsBtn(titleArr[0], picName: nil, btnBackColor: nil, titleColor: LDFontColor.DeepGray)
        voiceResumeBtn?.tag = btnsTag + 0
        voiceResumeBtn?.bk_whenTapped({ [weak self] in
            self?.clickBtn((self?.voiceResumeBtn)!)
            
            })
        
        textResumeBtn = UIButton.setDetailsBtn(titleArr[1], picName: nil, btnBackColor: nil, titleColor: LDFontColor.DeepGray)
        textResumeBtn?.tag = btnsTag + 1
        textResumeBtn?.bk_whenTapped({ [weak self] in
            self?.clickBtn((self?.textResumeBtn)!)
            
            })
        
        line1 = UIView.init()
        line1?.backgroundColor = LDIconColor.LightGray
        
        bottomLine = UIView.init()
        bottomLine?.backgroundColor = LDFontColor.Green
        
        view.addSubview(contentView!)
        view.addSubview(gampView!)
        gampView?.addSubview(voiceResumeBtn!)
        gampView?.addSubview(textResumeBtn!)
        gampView?.addSubview(line1!)
        view?.addSubview(bottomLine!)
        
        gampView?.anchorAndFillEdge(.Top, xPad: 0, yPad: 0, otherSize: BtnH)
        
        voiceResumeBtn?.anchorToEdge(.Left, padding: 0, width: BtnW, height: BtnH)
        line1?.align(.ToTheRightCentered, relativeTo: voiceResumeBtn!, padding: 0, width: 1, height: BtnH)
        textResumeBtn?.align(.ToTheRightCentered, relativeTo: line1!, padding: 0, width: BtnW, height: BtnH)
        bottomLine?.align(.UnderMatchingLeft, relativeTo: voiceResumeBtn!, padding: 0, width: BtnW, height: 2)
    }
    //视图内容
    func setupContentVC() {
        
        let voiceVC = LDEditMyResumeVoiceResumeVC()
        voiceVC.type = editType
        childVC.append(voiceVC)
        addChildViewController(voiceVC)
        
        let textVC = LDEditMyResumeTextResumeVC()
        textVC.type = editType
        childVC.append(textVC)
        addChildViewController(textVC)
        
    }
    //选中状态
    func clickBtn(btn:UIButton) {
        
        if preTitleButton == btn {
            
            return
        } else {
            
            preTitleButton = btn
        }
        
        
        changeBtnStatus(btn)
        let index = btn.tag - btnsTag
        let vc = childVC[index]
        
        if selectTitleButton.tag == voiceResumeBtn!.tag {
            
            LDUserInfoNet.sharedInstance.changeMyResumeVoiceORText(.Voice, success: nil, failure: nil)
            
        } else {
            
            LDUserInfoNet.sharedInstance.changeMyResumeVoiceORText(.Text, success: nil, failure: nil)
            
        }
        
        
        UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseInOut, animations: {
            [weak self] in
            self?.contentView?.contentOffset = CGPointMake(CGFloat(index)*LDFrame.ScreenWidth, 0)
            self?.bottomLine?.frame.origin.x = CGFloat(index)*(self?.BtnW)!
            }, completion: nil)
        
        if vc.view.superview != nil {
            return
        }
        vc.view.frame = CGRectMake(CGFloat(index)*LDFrame.ScreenWidth, 0, LDFrame.ScreenWidth, (contentView?.height)!)
        contentView?.addSubview(vc.view)
        
    }
    
    func changeBtnStatus(btn:UIButton) {
        btn.setTitleColor(LDFontColor.Green, forState: .Normal)
        selectTitleButton.setTitleColor(LDFontColor.DeepGray, forState: .Normal)
        selectTitleButton = btn
    }
    
    
}


extension LDEditMyResumeVC:UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let offIndex = (((contentView?.contentOffset.x)! % LDFrame.ScreenWidth) / LDFrame.ScreenWidth) > 0.5 ? 1:0
        let pageIndex = Int((contentView?.contentOffset.x)! / LDFrame.ScreenWidth) + offIndex
        
        let button = view.viewWithTag(btnsTag+pageIndex) as?UIButton
        //        debugPrint(button)
        
        if button != nil {
            clickBtn(button!)
        }
    }
}

